package com.example.LibrarySystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.LibrarySystem.annotation.RoleAnnotation;
import com.example.LibrarySystem.mapper.BookClassesMapper;
import com.example.LibrarySystem.pojo.BookClasses;
import com.example.LibrarySystem.pojo.Preeses;
import com.example.LibrarySystem.pojo.User;
import com.example.LibrarySystem.response.Result;
import com.example.LibrarySystem.service.BookClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bookclasses")
public class BookClassesController {
    @Autowired
    private BookClassesService bookClassesService;
    @Autowired
    private BookClassesMapper bookClassesMapper;

    @PostMapping("/insert")
    @RoleAnnotation(type = "admin")
    public Result insert(@RequestBody BookClasses bookClasses) {
        int res = bookClassesMapper.insert(bookClasses);
        if (res > 0) {
            return new Result("添加成功", 200);
        }
        return new Result("添加失败请重试", -1);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @RoleAnnotation(type = "admin")
    public Result deleteById(@PathVariable Integer id) {
        int res = bookClassesMapper.delById(id);
        if (res > 0) {
            return new Result("删除成功", 200);
        }
        return new Result("删除失败请重试", -1);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RoleAnnotation(type = "admin")
    public Result update(@RequestBody BookClasses bookClasses) {
        QueryWrapper<BookClasses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",bookClasses.getId());
        int res = bookClassesMapper.update(bookClasses, queryWrapper);
        if (res > 0) {
            return new Result("修改成功", 200);
        }
        return new Result("修改失败请重试", -1);
    }

    /**
     * 查询所有
     */
    @GetMapping("/getBookClassesByAll")
    public Result getBookClassesByAll(@RequestParam Map<String,String> data) {
        Result result = new Result();

        IPage<BookClasses> userByAll = bookClassesService.getBookClassesByAll(data);
        result.put("data",userByAll.getRecords());
        result.put("total",userByAll.getTotal());
        result.setMsg("查询成功");
        result.setStatus(200);
        return result;
    }
}
